package com.csit.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;

import com.csit.bean.Pager;
import com.csit.dao.BaseDao;
import com.csit.service.BaseService;

/**
 * @author longweier
 * @Description: 基类Service实现类
 * @Copyright: 福州骏华科技信息有限公司 (c)2012
 * @Created Date : 2012-3-29
 * @vesion 1.0
 */
public class BaseServiceImpl<T, PK extends Serializable> implements BaseService<T, PK> {

	protected static final String SPLIT_REGEX = "\\^";
	private BaseDao<T, PK> baseDao;

	public BaseDao<T, PK> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao<T, PK> baseDao) {
		this.baseDao = baseDao;
	}

	/*
		 * (non-Javadoc)
		 * @see com.csit.service.BaseService#clear()
		 */
	public void clear() {

		baseDao.clear();
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#deleteEntity(java.lang.Object)
	 */
	public void deleteEntity(T entity) {
		baseDao.delete(entity);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#deleteEntity(java.io.Serializable)
	 */
	public void deleteEntity(PK id) {

		baseDao.delete(id);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#deleteEntity(PK[])
	 */
	public void deleteEntity(PK[] ids) {
		baseDao.delete(ids);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#evict(java.lang.Object)
	 */
	public void evict(Object object) {
		baseDao.evict(object);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#findByPager(com.csit.bean.Pager)
	 */
	public Pager findByPager(Pager pager,Boolean countTotal) {

		return baseDao.findByPager(pager,countTotal);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#findByPager(com.csit.bean.Pager, org.hibernate.criterion.DetachedCriteria)
	 */
	public Pager findByPager(Pager pager, T entity,Boolean countTotal) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entity.getClass());
		return baseDao.findByPager(pager, detachedCriteria,countTotal);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#flush()
	 */
	public void flush() {

		baseDao.flush();
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#getAll()
	 */
	public List<T> getAll() {

		return baseDao.getAll();
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#getEntity(java.io.Serializable)
	 */
	public T getEntity(PK id) {

		return baseDao.get(id);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#getEntity(java.lang.String, java.lang.Object)
	 */
	public T getEntity(String propertyName, Object value) {

		return baseDao.get(propertyName, value);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#getEntityList(PK[])
	 */
	public List<T> getEntityList(PK[] ids) {

		return baseDao.get(ids);
	}

	public List<T> getEntityList(String hql, Object... params) {
		return baseDao.getEntityList(hql, params);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#getList(java.lang.String, java.lang.Object)
	 */
	public List<T> getList(String propertyName, Object value) {

		return baseDao.getList(propertyName, value);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#getTotalCount()
	 */
	public Long getTotalCount() {

		return baseDao.getTotalCount();
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#isEntityExist(java.lang.String, java.lang.Object)
	 */
	public boolean isEntityExist(String propertyName, Object value) {

		return baseDao.isExist(propertyName, value);
	}
	
	public boolean isEntityExist(String[] propertiesName, Object value) {
		
		return baseDao.isExist(propertiesName, value);
	}

	
	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#isEntityUnique(java.lang.String, java.lang.Object, java.lang.Object)
	 */
	public boolean isEntityUnique(String propertyName, Object oldValue, Object newValue) {

		return baseDao.isUnique(propertyName, oldValue, newValue);
	}

	/**
	 * 
	 */
	public boolean isEntityUnique(String[] propertiesName, Object oldValue, Object newValue){
		return baseDao.isUnique(propertiesName, oldValue, newValue);
	}
	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#loadEntity(java.io.Serializable)
	 */
	public T loadEntity(PK id) {

		return baseDao.load(id);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#saveEntity(java.lang.Object)
	 */
	public PK saveEntity(T entity) {

		return baseDao.save(entity);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#updateEntity(java.lang.Object)
	 */
	public void updateEntity(T entity) {

		baseDao.update(entity);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#findByPager(com.csit.bean.Pager, java.lang.String[], java.lang.Object)
	 */
	public Pager findByPager(Pager pager, String[] propertys, T entity,Boolean countTotal) {

		return baseDao.findByPager(pager, propertys, entity, countTotal);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.core.service.BaseService#getList(java.util.Map)
	 */
	public List<T> getList(Map<String, Object> params) {

		return baseDao.getList(params);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.service.BaseService#updateProperty(java.lang.String[], java.lang.Object, java.io.Serializable)
	 */
	public void updateProperty(String[] propertys, T entity, PK id) {

		T oldEntity = baseDao.get(id);

		baseDao.updateProperty(propertys, entity, oldEntity);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.core.service.BaseService#getList(java.lang.String[], java.lang.Object)
	 */
	public List<T> getList(String[] propertiesName, Object value) {
		return baseDao.getList(propertiesName,value);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.core.service.BaseService#merge(java.lang.Object)
	 */
	public void merge(T entity) {
		baseDao.merge(entity);
	}

	/*
	 * (non-Javadoc)   
	 * @see com.csit.core.service.BaseService#find(java.lang.Integer, java.lang.Integer, java.lang.String[], java.lang.Object)
	 */
	public List<T> find(Integer pageNumber, Integer pageSize,String[] propertys, T entity) {
		return baseDao.find(pageNumber, pageSize, propertys, entity);
	}
}
